<?php

session_start();

require "include/template2.inc.php";
require "include/beContent.inc.php";

require "include/auth.inc.php";

Class PubForm extends Form {
	
	function PubForm($name, $entity) {
		Form::Form($name, $entity);
	}
	
	function addItem_postInsertion() {
		
		$oid = mysql_query("INSERT INTO {$GLOBALS['pubAuthors']->name} 
		                    VALUES('{$this->lastid}','{$_SESSION['user']['username']}')");
		
		if (is_array($this->relationData[$GLOBALS['pubAuthors']->name])) {
		
			foreach ($this->relationData[$GLOBALS['pubAuthors']->name] as $user) {
			
				$data = aux::getResult("SELECT * FROM users WHERE username = '{$user[1]}'");
			
				$mail = new Template("skins/dipartimento/dtml/publication.mail");
				$mail->setContent("name", $data[0]['name']);
				$mail->setContent("title", $_REQUEST['title']);
				$mail->setContent("authors", $_REQUEST['authors']);
				$mail->setContent("venue", $_REQUEST['venue']);
				$mail->setContent("year", $_REQUEST['year']);
				$mail->setContent("id", $this->lastid);
					
				aux::mail($data[0]['email'], "{$_SESSION['user']['name']} {$_SESSION['user']['surname']} ha aggiunto una pubblicazione", $mail->get(), $_SESSION['user']['email']);
			}
		}
	}
}



$main = new Skin(); 

$form = new PubForm("dataEntry",$pubEntity);


$form->addSection("Publication Management");



$form->addText("title", "Title", 70, MANDATORY);
$form->addText("authors", "Authors", 70, MANDATORY);

if ($_SESSION['user']['admin']) {
	$form->addSelectFromReference2($usersEntity, "username", "User", MANDATORY);	
}


$form->addSelectFromReference2($pubtypeEntity, "type", "Type", MANDATORY);
$form->addTextarea("abstract", "Abstract", 10, 80);

$form->addFile("pdf", "Pdf");
$form->setFilterByExtension("pdf", "pdf", aux::lingual("sono ammessi solo documenti in pdf !", "please only pdf documents allowed !"));

$form->addYear("year", "Year");
$form->setMandatory("year");
$form->addTextarea("venue", "Details", 5, 80);

$form1 = new PubForm("rel", $pubAuthors);
$form1->addRelationManager("authors", "Authors");
$form1->restrictReference("authors", "username <> '{$_SESSION['user']['username']}'");


$form1->addDescription("By clicking below, you allow the publication to appear also in the homepage of your co-authors.");

$form->triggers($form1);



if (!isset($_REQUEST['action'])) {
	$_REQUEST['action'] = "edit";
}
switch($_REQUEST['action']) {
	case "add":
	$main->setContent("body",$form->addItem());
	break;
	case "edit":
	$main->setContent("body",$form->editItem());
	break;
}


$main->close();

?> 